<?php

/**
 *  @FileComment            [商品分类后台控制器]
 *  @ProjectName             项目名称：shopFrame
 *  @FileName                   文件名称：CategoryController.class.php
 *  @Author                       yuerengui <596580041@qq.com>
 *  @Date                          2014-10-9 18:00:45
 *  *
 */
class CategoryController extends CommonController {

        private $model;

        public function __init() {
                parent::__init();
                $this->model = K('Category');
        }

        public function index() {
                $data = $this->model->tree_data();
                $this->assign('data', $data);
                $this->display();
        }

        //添加分类
        public function add() {
                if (IS_POST) {
                        if ($this->model->create()) {
                                $pid = Q('post.pid', 0, 'intval');
                                $this->model->add_data($pid);
                                $this->success('添加分类成功！', U('index'));
                        } else {
                                $this->error($this->model->error);
                        }
                }
                $data = K('Type')->all();
                $this->assign('data', $data);
                $this->display();
        }

        //编辑分类
        public function edit() {
                if (IS_POST) {
                        if ($this->model->create()) {
                                $this->model->edit_data();
                        } else {
                                $this->error($this->model->error);
                        }
                }
                //获取当前的分类信息
                $cid = Q('get.cid', 0, 'intval');
                $cateInfo = $this->model->get_data($cid);
                $this->assign('cateInfo', $cateInfo);
                //获取所有类型
                $data = K('Type')->all();
                $this->assign('data', $data);
                $cateAll = $this->model->get_tree_data($cateInfo['type_tid']);
                $this->assign('cateAll', $cateAll);
                $this->display();
        }

        //添加子分类
        public function add_son() {
                if (IS_POST) {
                        if ($this->model->create()) {
                                $pid = Q('post.pid', 0, 'intval');
                                $this->model->add_data($pid);
                                $this->success('添加子分类成功！', U('index'));
                        } else {
                                $this->error($this->model->error);
                        }
                }
                //商品的所属分类
                $pid = Q('get.pid', 0, 'intval');
                $data = $this->model->get_data($pid);
                $this->assign('cateNow', $data);
                //商品所属类型
                $data = K('Type')->get_one_data($data['type_tid']);
                $this->assign('data', $data);
                $this->display();
        }

        public function del() {
                $cid = Q('get.cid', 0, 'intval');
                if ($this->model->del_data($cid)) {
                        $this->success('删除分类成功！', U('index'));
                } else {
                        $this->error($this->model->error);
                }
        }

        /**
         *  [添加分类时点击类型ajax抓取类型下面的分类]
         */
        public function ajax_cate() {
                $tid = Q('post.tid', 0, 'intval');
                $data = $this->model->get_tree_data($tid);
                $tempAttr = array();
                foreach ($data as $v) {
                        $tempAttr[] = $v;
                }
                $this->ajax($tempAttr);
        }

}
